//
//  EFengShareCell.m
//  WZEfengAndEtong
//
//  Created by wanzhao on 16/2/29.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "EFengShareCell.h"
#import "EFengShare.h"
#import "EFengPhotosView.h"
#import "EFengshareCommentsView.h"
#import "UIImageView+WebCache.h"
#import "CXmppRequestMgr.h"
#import "EFengShareFrame.h"
#import "EFengCommentsFrame.h"
#import "EFengPhotosFrame.h"
#import "UIUtil.h"
#import "UIButton+WebCache.h"
#import "WZAttrbuteLabel.h"
#import "NSAttributedString+ZBTension.h"
#import "CatZanButton.h"

@interface EFengShareCell ()<EFengshareCommentsViewDelegate,CatzanButtonDelegate>

WeakProperty EFengPhotosView *photosView;
WeakProperty EFengshareCommentsView *commentsView;
WeakProperty UIButton *headIcon;
WeakProperty UILabel *nameLabel;
WeakProperty UILabel *timeLabel;
WeakProperty WZAttrbuteLabel *contentLabel;

WeakProperty UIButton *openButton;

WeakProperty CatZanButton *zanButton;
WeakProperty UIButton *huifuButton;
WeakProperty UIButton *shanchuButton;

WeakProperty UIView *toolView;
WeakProperty WZAttrbuteLabel *zanLabel;
WeakProperty UIImageView *commentView;
WeakProperty UIView *commentLine;

WeakProperty UIView *line;

@end

@implementation EFengShareCell

+ (instancetype)eFengShaeCellWithTableView:(UITableView *)tableView
{
    static NSString *shareCellId = @"eFengShareCell";
    EFengShareCell *cell = [tableView dequeueReusableCellWithIdentifier:shareCellId];
    if (cell == nil) {
        cell = [[EFengShareCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:shareCellId];
    }
    return cell;
}
-(void)CatZanButtonClick:(CatZanButton *)sender
{
    [self zanButtonClick];
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = WhiteColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIButton *img = [UIButton buttonWithType:UIButtonTypeCustom];
        img.titleLabel.numberOfLines = 0;
        img.titleLabel.textAlignment = UITextAlignmentCenter;
        [img addTarget:self action:@selector(didClickHead) forControlEvents:UIControlEventTouchUpInside];
        img.userInteractionEnabled = YES;
        img.layer.cornerRadius = 3.0f;
        img.layer.masksToBounds = YES;
        [self.contentView addSubview:img];
        self.headIcon = img;
        
        UILabel *name = [[UILabel alloc] init];
        name.userInteractionEnabled = YES;
        name.textAlignment = UITextAlignmentLeft;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickHead)];
        [name addGestureRecognizer:tap];
        name.font = Font16;
        name.textColor = RGBCOLORWITH16COLOR(0x1e628f);
        [self.contentView addSubview:name];
        self.nameLabel = name;
        
        UILabel *timeLabel = [[UILabel alloc] init];
        timeLabel.textAlignment = UITextAlignmentRight;
        timeLabel.font = Font14;
        timeLabel.textColor = RGBCOLORWITH16COLOR(0xacacac);
        [self.contentView addSubview:timeLabel];
        self.timeLabel = timeLabel;

        WZAttrbuteLabel *contentLabel = [[WZAttrbuteLabel alloc] init];
        contentLabel.userInteractionEnabled = YES;
//        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(didLongPress:)];
//        longPress.minimumPressDuration = 0.4f;
//        [contentLabel addGestureRecognizer:longPress];
        contentLabel.numberOfLines = 0;
        contentLabel.textAlignment = UITextAlignmentLeft;
        contentLabel.font = Font16;
        contentLabel.textColor = BlackColor;
        [self.contentView addSubview:contentLabel];
        self.contentLabel = contentLabel;
        
        UIButton *openButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [openButton setImage:[UIImage imageNamed:@"aaaa"] forState:UIControlStateNormal];
        openButton.titleLabel.font = Font14;
        [openButton setTitleColor:RGBCOLORWITH16COLOR(0x1e628f) forState:UIControlStateNormal];
        [openButton setTitle:@"展开" forState:UIControlStateNormal];
        [openButton addTarget:self action:@selector(openButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
        openButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        [self.contentView addSubview:openButton];
        self.openButton = openButton;

        
        EFengPhotosView *photosView = [[EFengPhotosView alloc] init];
        [self.contentView addSubview:photosView];
        self.photosView = photosView;
        
        UIView *toolView = [[UIView alloc] init];
        toolView.backgroundColor = ClearColor;
        [self.contentView addSubview:toolView];
        self.toolView = toolView;
        [self setupToolView];
        
        UIImageView *commentView = [[UIImageView alloc] init];
        commentView.userInteractionEnabled = YES;
        commentView.layer.cornerRadius = 3.0f;
        commentView.layer.masksToBounds = YES;
        commentView.backgroundColor = RGBCOLOR(248, 248, 248);
        [self.contentView addSubview:commentView];
        self.commentView = commentView;
        
        UIView *commentLine = [[UIView alloc] init];
        commentLine.backgroundColor = RGBCOLOR(238, 238, 238);
        [self.commentView addSubview:commentLine];
        self.commentLine = commentLine;
        
        WZAttrbuteLabel *zanLabel = [[WZAttrbuteLabel alloc] init];
        zanLabel.numberOfLines = 0;
        zanLabel.textAlignment = UITextAlignmentLeft;
        zanLabel.font = Font14;
        zanLabel.textColor = BlackColor;
        [self.commentView addSubview:zanLabel];
        self.zanLabel = zanLabel;

        
        EFengshareCommentsView *commentsView = [[EFengshareCommentsView alloc] init];
        commentsView.eFengshareCommentsViewDelegate = self;
        [self.commentView addSubview:commentsView];
        self.commentsView = commentsView;
        
        
        UIView *line = [[UIView alloc] init];
        line.backgroundColor = RGBCOLOR(212, 212, 212);
        [self.contentView addSubview:line];
        self.line = line;
    }
    return self;
}

- (void)setupToolView
{
    UIButton *shanchu = [UIButton buttonWithType:UIButtonTypeCustom];
    [shanchu setImage:[UIImage imageNamed:@"aaaa"] forState:UIControlStateNormal];
    shanchu.titleLabel.font = Font14;
    [shanchu setTitleColor:RGBCOLORWITH16COLOR(0x1e628f) forState:UIControlStateNormal];
    [shanchu setTitle:@"删除" forState:UIControlStateNormal];
    [shanchu addTarget:self action:@selector(shanchuButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:shanchu];
    self.shanchuButton = shanchu;
    
    CatZanButton *zanBtn=[[CatZanButton alloc] init];
    zanBtn.deleagate = self;
    [self.toolView addSubview:zanBtn];
    [zanBtn setType:CatZanButtonTypeFirework];
    self.zanButton = zanBtn;
    
    
    UIButton *huifu = [UIButton buttonWithType:UIButtonTypeCustom];
//    huifu.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [huifu setImage:[UIImage imageNamed:@"btn-talk"] forState:UIControlStateNormal];
    huifu.backgroundColor = ClearColor;
    huifu.titleEdgeInsets = UIEdgeInsetsMake(0, 5.0f, 0, 0);
    huifu.titleLabel.font = Font14;
//    [huifu setTitle:@"2" forState:UIControlStateNormal];
    [huifu setTitleColor:RGBCOLORWITH16COLOR(0xacacac) forState:UIControlStateNormal];
    [huifu addTarget:self action:@selector(huifuButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:huifu];
    self.huifuButton = huifu;
    
}

- (void)didClickHead
{
    if ([self.eFengShareCellDelegate respondsToSelector:@selector(eFengShareCellDidClickHeadWithShare:indexPath:)] && self.eFengShareCellDelegate != nil) {
        [self.eFengShareCellDelegate eFengShareCellDidClickHeadWithShare:self.shareFrame indexPath:self.cellIndexPath];
    }

}
- (void)shanchuButtonClick
{
    if ([self.eFengShareCellDelegate respondsToSelector:@selector(eFengShareCellDidClickShanChuWithShare:indexPath:)] && self.eFengShareCellDelegate != nil) {
        [self.eFengShareCellDelegate eFengShareCellDidClickShanChuWithShare:self.shareFrame indexPath:self.cellIndexPath];
    }
}

- (void)zanButtonClick
{
       if ([self.eFengShareCellDelegate respondsToSelector:@selector(eFengShareCellDidClickZanWithShare:indexPath:)] && self.eFengShareCellDelegate != nil) {
        [self.eFengShareCellDelegate eFengShareCellDidClickZanWithShare:self.shareFrame indexPath:self.cellIndexPath];
    }

}

- (void)huifuButtonClick
{
    if ([self.eFengShareCellDelegate respondsToSelector:@selector(eFengShareCellDidClickHuiFuWithShare:indexPath:)] && self.eFengShareCellDelegate != nil) {
        [self.eFengShareCellDelegate eFengShareCellDidClickHuiFuWithShare:self.shareFrame indexPath:self.cellIndexPath];
    }

}

- (void)openButtonDidClick
{
    if ([self.eFengShareCellDelegate respondsToSelector:@selector(eFengShareCellDidClickOpenWithShare:indexPath:)] && self.eFengShareCellDelegate != nil) {
        [self.eFengShareCellDelegate eFengShareCellDidClickOpenWithShare:self.shareFrame indexPath:self.cellIndexPath];
    }

}

- (void)setShareFrame:(EFengShareFrame *)shareFrame
{
    _shareFrame = shareFrame;
    EFengShare *share = shareFrame.share;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:share.date.longLongValue / 1000.0];
    if (share.isFromMineShare) {
        
        [self.headIcon setAttributedTitle:[UIUtil timeForMineShareListStyle:date] forState:UIControlStateNormal];
        self.nameLabel.text = @"";
        self.timeLabel.text = @"";

    } else {
        self.nameLabel.text = share.releaseUserName;
        self.timeLabel.text = [UIUtil timeForMessageListStyle:date];
        [self.headIcon sd_setBackgroundImageWithURL:[NSURL URLWithString:[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:share.releaseUserId]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"80-80"]];
 
    }
    self.headIcon.frame = shareFrame.headFrame;
    
    self.nameLabel.frame = shareFrame.nameFrame;
    
    self.timeLabel.frame = shareFrame.timeFrame;
    
    self.contentLabel.attributedText = share.attributedText;
    self.contentLabel.pasteStr = share.content;
    self.contentLabel.frame = shareFrame.contentFrame;
    
    if (share.isShowOpenOrHide) {
        self.openButton.hidden = NO;
        if (share.isOpen) {
            [self.openButton setTitle:@"收起" forState:UIControlStateNormal];
        } else {
            [self.openButton setTitle:@"展开" forState:UIControlStateNormal];
        }
    } else {
        self.openButton.hidden = YES;
    }
    
    self.openButton.frame = shareFrame.openFrame;
    self.photosView.photosFrame = shareFrame.photosFrame;
    
    self.commentView.frame = shareFrame.commentViewFrame;
    self.commentLine.frame = shareFrame.commentlineFrame;
    
    self.toolView.frame = shareFrame.toolFrame;
    self.shanchuButton.hidden = !share.isShowShanchu;
    
    self.shanchuButton.frame = CGRectMake(0, 0, 40.0f, shareFrame.toolFrame.size.height);
    
    
    if (share.contentReply.count > 0) {
        [self.huifuButton setTitle:[NSString stringWithFormat:@"%zd",share.contentReply.count] forState:UIControlStateNormal];
    } else {
        [self.huifuButton setTitle:@"" forState:UIControlStateNormal];
    }
    self.huifuButton.frame = CGRectMake(shareFrame.toolFrame.size.width - 50.0f, 0, 50.0f, shareFrame.toolFrame.size.height);
    
    self.zanButton.isZan = share.isZan;
    self.zanButton.frame = CGRectMake(self.huifuButton.x - 35.0f , 6, 16, 16);
    CGPoint point = self.zanButton.center;
    point.y = _huifuButton.center.y;
    self.zanButton.center = point;
    
    if ([share.praiseArray count] > 0) {
        self.zanButton.countLabel.text = [NSString stringWithFormat:@"%zd",share.praiseArray.count];
    } else {
        self.zanButton.countLabel.text = @"";
    }

    self.zanLabel.attributedText = share.zanAttrText;
    self.zanLabel.frame = shareFrame.praiseFrame;
    
    self.commentsView.commentsFrame = shareFrame.commentsFrame;
    
    self.line.frame = shareFrame.lineFrame;
}

- (void)didLongPress:(UILongPressGestureRecognizer *)longPress
{
    if(longPress.state != UIGestureRecognizerStateBegan || ![self becomeFirstResponder])
        return;
    UIMenuController *menu = [UIMenuController sharedMenuController];
    
    
    
    CGRect targetRect = [self convertRect:self.contentLabel.frame
                                 fromView:self.contentLabel];
    [menu setTargetRect:CGRectInset(targetRect, 0.0f, 4.0f) inView:self];
    [menu setMenuVisible:YES animated:YES];
    //    [self handleLongText];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleMenuWillShowNotification:)
                                                 name:UIMenuControllerWillShowMenuNotification
                                               object:nil];

}

#pragma mark - Copying
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (BOOL)becomeFirstResponder
{
    return [super becomeFirstResponder];
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    [super canPerformAction:action withSender:sender];
    if(action == @selector(copy:))
        return YES;
    //    if (action==@selector(delete:)) {
    //        return YES;
    //    }
    return NO;
}


- (void)copy:(id)sender
{
    [[UIPasteboard generalPasteboard] setString:self.shareFrame.share.content];
    NSLog(@"%@",self.shareFrame.share.content);
    [self resignFirstResponder];
}



#pragma mark - Notification
- (void)handleMenuWillHideNotification:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIMenuControllerWillHideMenuNotification
                                                  object:nil];
}

- (void)handleMenuWillShowNotification:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIMenuControllerWillShowMenuNotification
                                                  object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleMenuWillHideNotification:)
                                                 name:UIMenuControllerWillHideMenuNotification
                                               object:nil];
    
}


- (void)eFengshareCommentsViewDidHuifuWithComment:(EFengContent *)comment
{
    if (self.eFengShareCellDelegate != nil && [self.eFengShareCellDelegate respondsToSelector:@selector(eFengshareCommentsViewDidHuifuWithComment:share:indexPath:)]) {
        [self.eFengShareCellDelegate eFengshareCommentsViewDidHuifuWithComment:comment share:self.shareFrame indexPath:self.cellIndexPath];
    }
}
@end
